package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.Pay;
import com.gouaixin.bee.beeframework2.model.MemberModel;

//import com.gouaixin.bee.beeframework2.model.SmsModel;


public class PayWangyinActivity extends Activity {
    Context context = PayWangyinActivity.this;

    WebView webView;

    Pay pay;//传递过来的Pay订单信息
    String tradeName;//订单名称
    String amount;//订单金额

    String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.pay_web);
        webView = (WebView) findViewById(R.id.webView);

        Intent it = getIntent();
        pay=new Gson().fromJson(it.getStringExtra("pay"),Pay.class);
        Log.e("test", "PayWangyinActivity:传递的type=" + pay.getGoods_id());

        url = it.getStringExtra("url");
        Log.e("test","网银+接受到的url：" + it.getStringExtra("pay"));

        //发现分类的网页视图

        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptEnabled(true);

        webSetting.setUseWideViewPort(true);

        webView.requestFocus();
        webView.addJavascriptInterface(context, "jiekou");

        webView.loadUrl(url);

        webView.setWebViewClient(new HelloWebViewClient());


    }


    @JavascriptInterface
    public void chenggong() {
        Log.e("test", "网银+支付成功");
//        AbToastUtil.showToast(context,"支付成功，现在可以返回了！");
//        canPressBack=true;

        Log.e("test", "PayWangyinActivity:传递的type=" + pay.getGoods_id());

//        Log.e("test", "支付成功，canPressBack="+canPressBack);


        MemberModel.doBusinessWithGoodid(context, pay.getGoods_id());






    }


    @JavascriptInterface
    public void shibai() {
        Log.e("test", "网银+支付失败");

//        AbToastUtil.showToast(context,"支付失败，现在可以返回了！");
//        canPressBack=true;

//        Log.e("test", "支付失败，canPressBack="+canPressBack);



        finish();
    }


    //WebView客户端
    public static class HelloWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e("test", "onPageFinished：url=" + url);

            view.requestFocus();
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Log.e("test", "onReceivedError：errorCode=" + errorCode);
        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.e("test", "shouldOverrideUrlLoading：url=" + url);
            return false;


        }
    }

     boolean canPressBack=false;//不可返回

//    @Override
//    public void onBackPressed() {
//        if(canPressBack==false){
//            AbToastUtil.showToast(context,"暂不可返回，请点击返回商户！");
//
//        }else {
//            AbToastUtil.showToast(context,"你点击了返回！准备返回！");
//            finish();
//        }
//    }
}
